net: report the kernel-assigned port after listening on port 0 - #80
net: report the kernel-assigned port after listening on port 0#800pcom wants to merge 2 commits into
Conversation
Listening on ":0" asks the kernel for an ephemeral port, but the listener kept the requested address, so ln.Addr() reported port 0 and the common pattern of binding an ephemeral port and dialing ln.Addr() (used by httptest.NewServer among others) could not work. Add GetSockname to the host netdev and look it up from listenTCP through an optional interface, so netdevs that cannot report the bound address are unaffected and keep the previous behavior. UDP does not have this problem: ListenUDP already fills in a port of its own before binding.
|
I integrated your 7bf5e52 into the fork and kept your authorship and source commit reference. One small finding: the new TCPAddr assignment drops laddr.Zone. Adding Tests and correction are available to use here. Both tests pass on Linux arm64 and native Darwin arm64 with released TinyGo 0.43.0-net.1, Go 1.26.7, isolated TINYGOROOT, and 15-second process limits. For integration with #74, Accept must retain No duplicate PR was opened. This remains your port fix. The fork's known close and poller limits are unchanged. |
(cherry picked from commit 0f46080)
|
Thank you — the Zone drop was real. Your 0f46080 is now cherry-picked here as 7b7aacb, authorship preserved. One adaptation: |
Listening on
:0asks the kernel for an ephemeral port, but the listener kept the requested address, soln.Addr()reported port 0 and the common pattern of binding an ephemeral port and dialingln.Addr()(used byhttptest.NewServeramong others) could not work.Add
GetSocknameto the host netdev and look it up fromlistenTCPthrough an optional interface, so netdevs that cannot report the bound address are unaffected and keep the previous behavior. UDP does not have this problem:ListenUDPalready fills in a port of its own before binding.Verified on linux/amd64 (tinygo dev):
Listen("127.0.0.1:0")now reports the real port and dialingln.Addr().String()connects.🤖 Generated with Claude Code
https://claude.ai/code/session_01C3X3gvq8ZggMRvzVzWm66i